package COMIC_BOOK_STORE;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * STAFF_ADD.java
 *
 * Created on Sep 23, 2010, 10:39:08 PM
 */

/**
 *
 * @author Alexander
 */
public class STAFF_ADD extends javax.swing.JFrame {

    Connection con;
    Statement stat;
    /** Creates new form STAFF_ADD */
    public STAFF_ADD() throws SQLException {
        super("ADD STAFF");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        staffComplexLabel = new javax.swing.JLabel();
        staffMobilePhoneBox = new javax.swing.JTextField();
        staffAddressDetailLabel = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        staffHouseLabel = new javax.swing.JLabel();
        staffMobilePhoneLabel = new javax.swing.JLabel();
        staffHousePhoneBox = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        staffAddConfirmButton = new javax.swing.JButton();
        staffGenderLabel = new javax.swing.JLabel();
        staffPrivilegeLabel = new javax.swing.JLabel();
        staffMonthComboBox = new javax.swing.JComboBox();
        staffPrivilegeComboBox = new javax.swing.JComboBox();
        staffYearComboBox = new javax.swing.JComboBox();
        staffPostcodeLabel = new javax.swing.JLabel();
        staffStreetLabel = new javax.swing.JLabel();
        staffBackButton = new javax.swing.JButton();
        staffCityLabel = new javax.swing.JLabel();
        staffStateLabel = new javax.swing.JLabel();
        staffHouseBox = new javax.swing.JTextField();
        staffComplexBox = new javax.swing.JTextField();
        staffStreetBox = new javax.swing.JTextField();
        staffPostcodeBox = new javax.swing.JTextField();
        staffCityBox = new javax.swing.JTextField();
        staffStateComboBox = new javax.swing.JComboBox();
        staffFirstNameLabel = new javax.swing.JLabel();
        staffPersonalDetailLabel = new javax.swing.JLabel();
        staffDayComboBox = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();
        staffLastNameBox = new javax.swing.JTextField();
        staffFirstNameBox = new javax.swing.JTextField();
        staffHousephoneLabel = new javax.swing.JLabel();
        staffICNumberLabel = new javax.swing.JLabel();
        staffPhoneDetailLabel = new javax.swing.JLabel();
        staffLastNameLabel = new javax.swing.JLabel();
        staffDOBLabel = new javax.swing.JLabel();
        staffICNumberBox = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        passwordTextField = new javax.swing.JTextField();
        staffCodeLabel = new javax.swing.JLabel();
        staffCodeTextField = new javax.swing.JTextField();
        genderComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        staffComplexLabel.setText("Complex/Garden");
        staffComplexLabel.setBounds(460, 100, 140, 20);
        jLayeredPane1.add(staffComplexLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffMobilePhoneBox.setBounds(140, 380, 150, 30);
        jLayeredPane1.add(staffMobilePhoneBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffAddressDetailLabel.setText("Address Details");
        staffAddressDetailLabel.setBounds(450, 20, 140, 30);
        jLayeredPane1.add(staffAddressDetailLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator3.setBounds(0, 0, 2, 0);
        jLayeredPane1.add(jSeparator3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffHouseLabel.setText("Apartment/House No.");
        staffHouseLabel.setBounds(460, 60, 160, 30);
        jLayeredPane1.add(staffHouseLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffMobilePhoneLabel.setText("Mobile");
        staffMobilePhoneLabel.setBounds(50, 380, 80, 30);
        jLayeredPane1.add(staffMobilePhoneLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffHousePhoneBox.setBounds(140, 340, 150, 30);
        jLayeredPane1.add(staffHousePhoneBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 0, 0, 2);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffAddConfirmButton.setText("ADD");
        staffAddConfirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                staffAddConfirmButtonActionPerformed(evt);
            }
        });
        staffAddConfirmButton.setBounds(600, 430, 100, 40);
        jLayeredPane1.add(staffAddConfirmButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffGenderLabel.setText("Gender");
        staffGenderLabel.setBounds(40, 210, 90, 30);
        jLayeredPane1.add(staffGenderLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffPrivilegeLabel.setText("Privilege Classification");
        staffPrivilegeLabel.setBounds(460, 330, 160, 30);
        jLayeredPane1.add(staffPrivilegeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffMonthComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        staffMonthComboBox.setBounds(210, 250, 90, 30);
        jLayeredPane1.add(staffMonthComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffPrivilegeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Admin", "Employee" }));
        staffPrivilegeComboBox.setBounds(640, 330, 120, 30);
        jLayeredPane1.add(staffPrivilegeComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffYearComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Year", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", " " }));
        staffYearComboBox.setBounds(310, 250, 80, 30);
        jLayeredPane1.add(staffYearComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffPostcodeLabel.setText("Postcode");
        staffPostcodeLabel.setBounds(460, 180, 140, 20);
        jLayeredPane1.add(staffPostcodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffStreetLabel.setText("Street/Road");
        staffStreetLabel.setBounds(460, 140, 150, 30);
        jLayeredPane1.add(staffStreetLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffBackButton.setText("BACK");
        staffBackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                staffBackButtonActionPerformed(evt);
            }
        });
        staffBackButton.setBounds(710, 430, 90, 40);
        jLayeredPane1.add(staffBackButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffCityLabel.setText("City");
        staffCityLabel.setBounds(460, 220, 80, 30);
        jLayeredPane1.add(staffCityLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffStateLabel.setText("State");
        staffStateLabel.setBounds(460, 260, 110, 30);
        jLayeredPane1.add(staffStateLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffHouseBox.setBounds(640, 60, 180, 30);
        jLayeredPane1.add(staffHouseBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffComplexBox.setBounds(640, 100, 180, 30);
        jLayeredPane1.add(staffComplexBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffStreetBox.setBounds(640, 140, 180, 30);
        jLayeredPane1.add(staffStreetBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffPostcodeBox.setBounds(640, 180, 180, 30);
        jLayeredPane1.add(staffPostcodeBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffCityBox.setBounds(640, 220, 180, 30);
        jLayeredPane1.add(staffCityBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffStateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Perlis", "Kedah", "Pulau Pinang", "Perak", "Kuala Lumpur", "Selangor", "Negeri Sembilan", "Melaka", "Pahang", "Johor", "Kelantan", "Terengganu", "Sabah", "Sarawak", " " }));
        staffStateComboBox.setBounds(640, 260, 130, 30);
        jLayeredPane1.add(staffStateComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffFirstNameLabel.setText("First Name");
        staffFirstNameLabel.setBounds(40, 84, 90, 30);
        jLayeredPane1.add(staffFirstNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffPersonalDetailLabel.setText("Personal Details");
        staffPersonalDetailLabel.setBounds(30, 20, 110, 20);
        jLayeredPane1.add(staffPersonalDetailLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffDayComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Day", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        staffDayComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                staffDayComboBoxActionPerformed(evt);
            }
        });
        staffDayComboBox.setBounds(140, 250, 60, 30);
        jLayeredPane1.add(staffDayComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator2.setBounds(0, 0, 0, 2);
        jLayeredPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffLastNameBox.setBounds(140, 130, 140, 30);
        jLayeredPane1.add(staffLastNameBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffFirstNameBox.setBounds(140, 90, 140, 30);
        jLayeredPane1.add(staffFirstNameBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffHousephoneLabel.setText("House");
        staffHousephoneLabel.setBounds(50, 340, 80, 20);
        jLayeredPane1.add(staffHousephoneLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffICNumberLabel.setText("IC Number");
        staffICNumberLabel.setBounds(40, 170, 90, 30);
        jLayeredPane1.add(staffICNumberLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffPhoneDetailLabel.setText("Phone Details");
        staffPhoneDetailLabel.setBounds(40, 300, 100, 30);
        jLayeredPane1.add(staffPhoneDetailLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffLastNameLabel.setText("Last Name");
        staffLastNameLabel.setBounds(40, 130, 90, 30);
        jLayeredPane1.add(staffLastNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffDOBLabel.setText("Date of Birth");
        staffDOBLabel.setBounds(40, 250, 90, 30);
        jLayeredPane1.add(staffDOBLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffICNumberBox.setBounds(140, 170, 140, 30);
        jLayeredPane1.add(staffICNumberBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        passwordLabel.setText("Password");
        passwordLabel.setBounds(460, 370, 130, 30);
        jLayeredPane1.add(passwordLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        passwordTextField.setBounds(640, 380, 120, 30);
        jLayeredPane1.add(passwordTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        staffCodeLabel.setText("staff Code");
        staffCodeLabel.setBounds(40, 54, 90, 20);
        jLayeredPane1.add(staffCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        staffCodeTextField.setBounds(140, 50, 140, 30);
        jLayeredPane1.add(staffCodeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genderComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "M", "F" }));
        genderComboBox.setBounds(140, 210, 100, 30);
        jLayeredPane1.add(genderComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setBounds(450, 310, 350, 110);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background3 copy.jpg"))); // NOI18N
        jLabel2.setBounds(0, 0, 900, 500);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-916)/2, (screenSize.height-537)/2, 916, 537);
    }// </editor-fold>                        

    private void staffAddConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                      

        String staff_c = staffCodeTextField.getText();
        String staff_Fname = staffFirstNameBox.getText();
        String staff_Lname = staffLastNameBox.getText();
        String staff_ic = staffICNumberBox.getText();
        String staff_dd = (String) staffDayComboBox.getSelectedItem();
        String staff_mm = (String) staffMonthComboBox.getSelectedItem();
        String staff_yy = (String) staffYearComboBox.getSelectedItem();
        String staff_g = (String) genderComboBox.getSelectedItem();
        String staff_house  = staffHouseBox.getText();
        String staff_complex = staffComplexBox.getText();
        String staff_street = staffStreetBox.getText();
        String staff_post = staffPostcodeBox.getText();
        String staff_city = staffCityBox.getText();
        String staff_state = (String) staffStateComboBox.getSelectedItem();
        String staff_hp = staffHousePhoneBox.getText();
        String staff_mp = staffMobilePhoneBox.getText();
        String staff_pri = (String) staffPrivilegeComboBox.getSelectedItem();
        String staff_p = passwordTextField.getText();
        String date = staff_dd+staff_mm+staff_yy;


        if(staff_c.equals("") || staff_Fname.equals("") || staff_Lname.equals("") || staff_ic.equals("") ||
           staff_house.equals("") || staff_complex.equals("") || staff_street.equals("") || staff_post.equals("") ||
           staff_city.equals("") || staff_hp.equals("") || staff_mp.equals("") || staff_p.equals(""))
        {
            JOptionPane.showMessageDialog(null, "Type something to add first!");
        }
        else
        {
        String query = "INSERT INTO STAFF VALUES('"+ staff_c +"','"+ staff_Fname +"','"+ staff_Lname +"',"+
                        "'"+ staff_ic +"','"+ date +"','"+staff_g +"','"+staff_house+"',"+
                        "'"+ staff_complex+"','"+staff_street+"','"+staff_post+"','"+staff_city+"',"+
                        "'"+ staff_state+"','"+staff_hp+"','"+staff_mp+"','"+staff_pri+"','"+staff_p+"')";
                        
        String message = "Are you sure to add?";
        String title = "ADD STAFF";

        int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);
        if(reply == JOptionPane.YES_OPTION)
        {
            
            try {

                stat = con.createStatement();
                stat.executeUpdate(query);
               
                JOptionPane.showMessageDialog(null, "Add Successful");
            }
            catch (SQLException ex)
            {
                Logger.getLogger(STAFF_ADD.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Add failed - some required fields are not filled, or bad input.");
            }
            clear();
        }
        }
        // TODO add your handling code here:
    }                                                     

    private void clear()
    {
        staffCodeTextField.setText("");
        staffFirstNameBox.setText("");
        staffLastNameBox.setText("");
        staffICNumberBox.setText("");
        staffDayComboBox.setSelectedIndex(0);
        staffMonthComboBox.setSelectedIndex(0);
        staffYearComboBox.setSelectedIndex(0);
        genderComboBox.setSelectedIndex(0);
        staffHouseBox.setText("");
        staffComplexBox.setText("");
        staffStreetBox.setText("");
        staffPostcodeBox.setText("");
        staffCityBox.setText("");
        staffStateComboBox.setSelectedIndex(0);
        staffHousePhoneBox.setText("");
        staffMobilePhoneBox.setText("");
        staffPrivilegeComboBox.setSelectedIndex(0);
        passwordTextField.setText("");
    }

    private void staffDayComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
    }                                                

    private void staffBackButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
        try {
            new STAFF_RECORD().setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(STAFF_ADD.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(false);// TODO add your handling code here:
    }                                               

    private void connect() throws SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");
        System.out.println("Database Connected");

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new STAFF_ADD().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(STAFF_ADD.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JComboBox genderComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JTextField passwordTextField;
    private javax.swing.JButton staffAddConfirmButton;
    private javax.swing.JLabel staffAddressDetailLabel;
    private javax.swing.JButton staffBackButton;
    private javax.swing.JTextField staffCityBox;
    private javax.swing.JLabel staffCityLabel;
    private javax.swing.JLabel staffCodeLabel;
    private javax.swing.JTextField staffCodeTextField;
    private javax.swing.JTextField staffComplexBox;
    private javax.swing.JLabel staffComplexLabel;
    private javax.swing.JLabel staffDOBLabel;
    private javax.swing.JComboBox staffDayComboBox;
    private javax.swing.JTextField staffFirstNameBox;
    private javax.swing.JLabel staffFirstNameLabel;
    private javax.swing.JLabel staffGenderLabel;
    private javax.swing.JTextField staffHouseBox;
    private javax.swing.JLabel staffHouseLabel;
    private javax.swing.JTextField staffHousePhoneBox;
    private javax.swing.JLabel staffHousephoneLabel;
    private javax.swing.JTextField staffICNumberBox;
    private javax.swing.JLabel staffICNumberLabel;
    private javax.swing.JTextField staffLastNameBox;
    private javax.swing.JLabel staffLastNameLabel;
    private javax.swing.JTextField staffMobilePhoneBox;
    private javax.swing.JLabel staffMobilePhoneLabel;
    private javax.swing.JComboBox staffMonthComboBox;
    private javax.swing.JLabel staffPersonalDetailLabel;
    private javax.swing.JLabel staffPhoneDetailLabel;
    private javax.swing.JTextField staffPostcodeBox;
    private javax.swing.JLabel staffPostcodeLabel;
    private javax.swing.JComboBox staffPrivilegeComboBox;
    private javax.swing.JLabel staffPrivilegeLabel;
    private javax.swing.JComboBox staffStateComboBox;
    private javax.swing.JLabel staffStateLabel;
    private javax.swing.JTextField staffStreetBox;
    private javax.swing.JLabel staffStreetLabel;
    private javax.swing.JComboBox staffYearComboBox;
    // End of variables declaration                   

}
